package com.agung.wonogirimaps;

import com.agung.wonogirimaps.adapter.TwoListItemAdapter;
import com.agung.wonogirimaps.util.Utils;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements
		OnItemClickListener
{

	// array untuk menu utama
	private final String[]		menuTitle		= { "Wisata Unggulan",
			"Daftar Hotel", "Daftar Rumah Makan", "About" };
	// array untuk icon menu
	private final int[]			menuThumbImg	= { R.drawable.ic_wisata,
			R.drawable.ic_hotel, R.drawable.ic_restaurant, R.drawable.ic_about };

	private TwoListItemAdapter	adapter;

	// listview pada menu utama
	private ListView			listMenu;

	private static final int	CEK_GPS			= 1;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// set layout ke acitivity_main.xml
		setContentView(R.layout.activity_main);

		listMenu = (ListView) findViewById(R.id.listMenu);
		listMenu.setOnItemClickListener(this);

		// set agar actionbar tidak menampilkan icon
		// hanya berupa text saja
		getSupportActionBar().setIcon(android.R.color.transparent);

		adapter = new TwoListItemAdapter(getApplicationContext(), menuTitle,
				menuThumbImg, R.layout.single_list_item_menu, R.id.thumbMenu,
				R.id.titleMenu);
		// tampilkan menu kedalam listview, set adapternya
		listMenu.setAdapter(adapter);

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3)
	{
		switch (position)
		{
			case 0:
				// startActivity(new Intent(this, LocationActivity.class));
				if (Utils.checkingGPS(this)
						&& Utils.isConnectedToInternet(this))
				{
					startActivity(new Intent(this, CategoryActivity.class));
					finish();
				} else
				{
					showSettingsAlert();
				}

				break;
			case 1:
				if (Utils.checkingGPS(this)
						&& Utils.isConnectedToInternet(this))
				{
					startActivity(new Intent(this, HotelActivity.class));
					finish();
				} else
				{
					showSettingsAlert();
				}

				break;
			case 2:
				if (Utils.checkingGPS(this)
						&& Utils.isConnectedToInternet(this))
				{
					startActivity(new Intent(this, RumahMakanActivity.class));
					finish();
				} else
				{
					showSettingsAlert();
				}

				break;
			case 3:
				startActivity(new Intent(this, AboutActivity.class));
				finish();
				break;

		}

	}

	private void showSettingsAlert()
	{
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("GPS Setting");
		alert.setMessage("Please activated your GPS !");

		alert.setPositiveButton("Settings",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						Intent i = new Intent(
								Settings.ACTION_LOCATION_SOURCE_SETTINGS);
						startActivityForResult(i, CEK_GPS);

					}
				});
		alert.setNegativeButton("Skip", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.cancel();
			}
		});

		if (Utils.isConnectedToInternet(this))
		{

			alert.show();
		} else
		{
			Toast.makeText(this, "Aplikasi ini membutuhkan koneksi internet",
					Toast.LENGTH_SHORT).show();
		}

	}
}
